﻿using System;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using Resources;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class KhoiLuongNew : System.Web.UI.Page
    {
        #region Properties

        private HopDongBiz _hopDongBiz;
        private HopDongBiz HopDongManager
        {
            get { return _hopDongBiz ?? (_hopDongBiz = new HopDongBiz()); }
        }

        private int _hopDongId;
        private int HopDongId
        {
            get
            {
                if (_hopDongId <= 0)
                    int.TryParse(Request.QueryString["hopDongId"], out _hopDongId);
                return _hopDongId;
            }
        }

        private HopDongView _hopDongView;
        private HopDongView CurrentHopDongView
        {
            get
            {
                if (_hopDongView == null && HopDongId > 0)
                {
                    _hopDongView = HopDongManager.GetById(HopDongId);
                }
                return _hopDongView;
            }
        }

        private KhoiLuongBiz _khoiLuongBiz;
        private KhoiLuongBiz KhoiLuongManager
        {
            get { return _khoiLuongBiz ?? (_khoiLuongBiz = new KhoiLuongBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentHopDongView == null)
                {
                    lblErrorMessage.Text = string.Format("{0}: Id={1}", GlobalStrings.NotExisted, HopDongId);
                    btnSave.Visible = false;
                    return;
                }

                LoadHopDongData();

                // Doi Thi Cong
                var doiThiCongs = DoiThiCongBiz.GetAll();
                doiThiCongs.Insert(0, new DoiThiCong { Id = 0, Ten = string.Empty });
                ddlDoiThiCong.DataTextField = "Ten";
                ddlDoiThiCong.DataValueField = "Id";
                ddlDoiThiCong.DataSource = doiThiCongs;
                ddlDoiThiCong.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;

            #region Validate inputs

            txtMaTuyen.Text = txtMaTuyen.Text.Trim();
            if (string.IsNullOrEmpty(txtMaTuyen.Text))
            {
                lblErrorMessage.Text = "Chưa nhập mã tuyến";
                return;
            }
            txtDiemDau.Text = txtDiemDau.Text.Trim();
            if (string.IsNullOrEmpty(txtDiemDau.Text))
            {
                lblErrorMessage.Text = "Chưa nhập điểm đầu";
                return;
            }
            txtDiemCuoi.Text = txtDiemCuoi.Text.Trim();
            if (string.IsNullOrEmpty(txtDiemCuoi.Text))
            {
                lblErrorMessage.Text = "Chưa nhập điểm cuối";
                return;
            }

            if (txtDiemDau.Text.Trim().ToLower().Equals(txtDiemCuoi.Text.Trim().ToLower()))
            {
                lblErrorMessage.Text = "Điểm đầu phải khác điểm cuối";
                return;
            }

            #endregion

            #region Collect inputs

            var khoiLuong = new KhoiLuongHopDong
            {
                HopDongId = HopDongId,
                MaTuyen = txtMaTuyen.Text.Trim(),
                TienDoThiCong = byte.Parse(ddlTienDo.SelectedValue),
                GhiChu = txtNote.Text,
                NhanVatTuA = byte.Parse(ddlNhanVatTuA.SelectedValue),
                DiemDau = txtDiemDau.Text.Trim(),
                DiemCuoi = txtDiemCuoi.Text.Trim(),
                LoaiCap = txtLoaiCap.Text.Trim(),
                DoiThiCongId = int.Parse(ddlDoiThiCong.SelectedValue)
            };

            double chieuDaiTuyen;
            if (double.TryParse(txtChieuDaiTuyen.Text.Trim(), out chieuDaiTuyen))
            {
                khoiLuong.ChieuDaiTuyen = chieuDaiTuyen;
            }

            #endregion

            // save
            KhoiLuongManager.CreateKhoiLuong(khoiLuong);

            Response.Redirect("KhoiLuong.aspx?hopDongId=" + HopDongId);
        }

        #endregion

        #region Helpers

        public void LoadHopDongData()
        {
            ltrTenDonVi.Text = CurrentHopDongView.TenDonVi;
            ltrSoHopDong.Text = CurrentHopDongView.SoHopDong;
            ltrLoaiDuAn.Text = CurrentHopDongView.LoaiDuAn;
        }

        #endregion
    }
}